package com.example.portal.tabel.controller;


import com.example.portal.tabel.service.TableServiceImpl;
import com.example.util.PageResult;
import com.example.util.ResponseMsg;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/table")
public class TableController {

   @Resource
   TableServiceImpl tableService;

    /**
     * 查询数据
     * @param params
     * @return
     */
    @RequestMapping("/getTables")
    public ResponseMsg getTables(@RequestParam Map<String,Object> params){
        try {

            PageResult pageResult= tableService.getTables(params);
            return ResponseMsg.success(pageResult.getData(),pageResult.getTotal());
        }catch (Exception e){
            e.printStackTrace();
            return ResponseMsg.fail(-1,e.getMessage());
        }
    }
    /**
     * 增加新的记录
     * @return
     */
    @RequestMapping("/insertTables")
    public ResponseMsg insertTables(@RequestBody Map<String,Object> params){
        try {
            tableService.insertTables(params);
            return ResponseMsg.success("success");
        }catch (Exception e){
            e.printStackTrace();
            return ResponseMsg.fail(-1,e.getMessage());
        }
    }

    /**
     * 根据id删除
     * @param param
     * @return
     */
    @RequestMapping("/delTables")
    public ResponseMsg delTables(@RequestBody Map<String,Object> param){
        try {
            tableService.delTables(param);
            return ResponseMsg.success("success");
        }catch (Exception e){
            e.printStackTrace();
            return ResponseMsg.fail(-1,e.getMessage());
        }
    }

    /**
     * 保存
     * @param params
     * @return
     */
    @RequestMapping("/updateTables")
    public ResponseMsg updateTables(@RequestBody Map<String,Object> params){
        try {
            tableService.updateTables(params);
            return ResponseMsg.success("success");
        }catch (Exception e){
            e.printStackTrace();
            return ResponseMsg.fail(-1,e.getMessage());
        }
    }


}
